package com.kymatrix.mpo.plus.bpm.server.model.form.instance;

import com.kymatrix.mpo.plus.bpm.server.model.form.process.QueryBpmProcessDefinitionResp;
import com.kymatrix.mpo.plus.common.util.DateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import lombok.Data;
import lombok.experimental.Accessors;
import org.flowable.engine.history.HistoricProcessInstance;

@ApiModel(value = "管理后台 - 流程实例的 Response")
@Data
@Accessors(chain=true)
public class QueryBpmProcessInstanceResp {

    @ApiModelProperty(value = "流程实例的编号", example = "1024")
    private String id;

    @ApiModelProperty(value = "流程名称", example = "zhangsan")
    private String name;

    @ApiModelProperty(value = "流程分类", example = "1")
    private String category;
    @ApiModelProperty(value = "流程分类名称", example = "请假")
    private String categoryName;

    @ApiModelProperty(value = "流程实例的状态", example = "1")
    private Integer status; // 参见 BpmProcessInstanceStatusEnum 枚举

    @ApiModelProperty(value = "发起时间")
    private LocalDateTime startTime;

    @ApiModelProperty(value = "结束时间")
    private LocalDateTime endTime;

    @ApiModelProperty(value = "持续时间", example = "1000")
    private Long durationInMillis;

    @ApiModelProperty(value = "提交的表单值")
    private Map<String, Object> formVariables;

    @ApiModelProperty(value = "业务的唯一标识-例如说，请假申请的编号", example = "1")
    private String businessKey;

    /**
     * 发起流程的用户
     */
    private User startUser;

    @ApiModelProperty(value = "流程定义的编号", example = "2048")
    private String processDefinitionId;
    /**
     * 流程定义
     */
    private QueryBpmProcessDefinitionResp processDefinition;

    /**
     * 当前审批中的任务
     */
    private List<Task> tasks; // 仅在流程实例分页才返回

    @ApiModel(value = "用户信息")
    @Data
@Accessors(chain=true)
    public static class User {

        @ApiModelProperty(value = "用户id", example = "1")
        private String id;
        @ApiModelProperty(value = "用户昵称", example = "芋艿")
        private String nickname;

        @ApiModelProperty(value = "部门id", example = "1")
        private String deptId;
        @ApiModelProperty(value = "部门名称", example = "研发部")
        private String deptName;

    }

    @ApiModel(value = "流程任务")
    @Data
@Accessors(chain=true)
    public static class Task {

        @ApiModelProperty(value = "流程任务的编号", example = "1024")
        private String id;

        @ApiModelProperty(value = "任务名称", example = "zhangsan")
        private String name;

    }
    
    public static QueryBpmProcessInstanceResp fromEntity(HistoricProcessInstance instance) {
        if (instance == null) {
            return null;
        }
        QueryBpmProcessInstanceResp res = new QueryBpmProcessInstanceResp();
        res.setId(instance.getId());
        res.setName(instance.getName());
        res.setStartTime(DateUtil.of(instance.getStartTime()));
        res.setEndTime(DateUtil.of(instance.getEndTime()));
        res.setDurationInMillis(instance.getDurationInMillis());
        res.setBusinessKey(instance.getBusinessKey());
        res.setProcessDefinitionId(instance.getProcessDefinitionId());
        
        
        return res;
        
    }

}
